package edu.ccut.saturn.workflow.start;


import java.util.HashMap;
import java.util.Map.Entry;

import edu.ccut.saturn.component.ComponentFactory;
import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.workflow.common.Constants;

public class WFAutoStart extends DefaultComponent {
	@SuppressWarnings("unchecked")
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		    String process_xml_key = (String)parameterContext.getParameterValue(0);
		    String version = (String)parameterContext.getParameterValue(1);
		    HashMap<String,String> paramMap = (HashMap<String,String>)parameterContext.getParameterValue(2);
		    String creator = (String)parameterContext.getParameterValue(3);
		    
        	debug("workflow has been started",parameterContext);
    		HashMap<String ,String > workFlowParamMap = new HashMap<String ,String >();
    		HashMap<String ,String > customParamMap = new HashMap<String ,String >();
    		workFlowParamMap.put("business_type", null);
    		workFlowParamMap.put("business_id", null);
    		workFlowParamMap.put("priority", null);
    		workFlowParamMap.put("name", null);
    		workFlowParamMap.put("description", null);
    		workFlowParamMap.put("creator_department", null);
    		workFlowParamMap.put("creator_name", null);
    		workFlowParamMap.put("creator", creator);
    		workFlowParamMap.put("process_xml_key", process_xml_key);
    		workFlowParamMap.put("version", version);
        	//处理初始化参数
        	IParameterContext logicParameterContext = ComponentFactory.createParameterContext();
        	for(Entry item :paramMap.entrySet()){
        		if(workFlowParamMap.containsKey(item.getKey())){
        			if("business_type".equals(item.getKey())||"business_id".equals(item.getKey())){
					    customParamMap.put(item.getKey()+"",(String)item.getValue()) ;
					}
        			workFlowParamMap.put(item.getKey()+"", (String)item.getValue());
        		}else{
        			customParamMap.put(item.getKey()+"",(String)item.getValue()) ;
        		}
        	}
        	
        	logicParameterContext.addParameter(workFlowParamMap, HashMap.class);
        	logicParameterContext.addParameter(customParamMap, HashMap.class);
			//action.execute(parameterContext);
        	String componentFullKey = Constants.WF_AUTOSTART_LOGIC;
            executeLogicSupportsTransaction(parameterContext, logicParameterContext, componentFullKey);
	}
}